import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Index } from "typeorm";
import { ManualTimestamp } from "../decorators/manual-timestamp.decorator";
import { CharacterEntity } from "./character.entity";

@Entity("player_online_time")
@ManualTimestamp()
@Index(["characterId", "date"], { unique: true }) // 每个角色每天只有一条记录
export class PlayerOnlineTimeEntity {
  @PrimaryGeneratedColumn({ comment: "主键ID" })
  id!: number;

  @Column({
    name: "character_id",
    type: "bigint",
    comment: "角色ID" })
  characterId!: number;

  @Column({
    name: "date",
    type: "date",
    comment: "日期（YYYY-MM-DD格式）" })
  date!: string;

  @Column({
    name: "login_time",
    type: "datetime",
    nullable: true,
    comment: "今日首次登录时间" })
  loginTime!: Date | null;

  @Column({
    name: "logout_time",
    type: "datetime",
    nullable: true,
    comment: "今日最后离线时间" })
  logoutTime!: Date | null;

  @Column({
    name: "total_online_minutes",
    type: "int",
    default: 0,
    comment: "今日累计在线时长（分钟）" })
  totalOnlineMinutes!: number;

  @Column({
    name: "is_online",
    type: "tinyint",
    default: 0,
    comment: "当前是否在线（1:在线, 0:离线）" })
  isOnline!: number;

  @Column({
    name: "last_activity_time",
    type: "datetime",
    nullable: true,
    comment: "最后活动时间" })
  lastActivityTime!: Date | null;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;
@Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;
// 关联关系
  // @ManyToOne => CharacterEntity, (character) => character.id)
  @JoinColumn({ name: "characterId" })
  character!: CharacterEntity;
}
